




//
//  LJHAppTools.m
//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHAppTools.h"

@implementation LJHAppTools

+(void)afnGetRequest:(NSString *)fileName block:(AppToolsBlook)block error:(errorBlock)errorBlock
{
     NSString *url_string = [fileName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        //        return responseObject;
        block(responseObject);
        
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [fileName hash]];
        // 存储的沙盒路径
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        // 归档
        [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        
        [GiFHUD dismiss];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        errorBlock(error);
        
        // 缓存的文件夹
        NSString *path = [NSString stringWithFormat:@"%ld.plist", [fileName hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        block(result);
        
        [GiFHUD dismiss];

    }];

    
    
}



@end
